using System;

namespace SubversionReports.Web.Controls
{
    public partial class RecordLinks : PagedControl
    {
        private int recordId;
        private int recordCount;
        private string recordIdentifier;

        /// <summary>
        /// Sets the record identifier.
        /// </summary>
        /// <value>The record identifier.</value>
        public string RecordIdentifier
        {
            set { recordIdentifier = value; }
        }

        /// <summary>
        /// Sets the record count.
        /// </summary>
        /// <value>The record count.</value>
        public int RecordCount
        {
            set { recordCount = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(recordIdentifier) && Request[recordIdentifier] != null)
            {
                recordId = int.Parse(Request[recordIdentifier]);

                if (recordCount > 0)
                {
                    LastLink.NavigateUrl = string.Format("{0}?{1}={2}", Request.FilePath, recordIdentifier, recordCount);
                    LastImageLink.NavigateUrl = LastLink.NavigateUrl;
                    LastLink.Enabled = (recordId < recordCount);
                    LastImageLink.Enabled = LastLink.Enabled;
                    LastImageLink.ImageUrl = LastImageLink.Enabled ? "~/Images/last-active.png" : "~/Images/last-inactive.png";

                    NextLink.Enabled = (recordId < recordCount);
                    NextImageLink.Enabled = NextLink.Enabled;
                    NextImageLink.ImageUrl = NextImageLink.Enabled ? "~/Images/next-active.png" : "~/Images/next-inactive.png";

                    FirstLink.Enabled = !recordId.Equals(1);
                    FirstImageLink.Enabled = FirstLink.Enabled;
                    FirstImageLink.ImageUrl = FirstImageLink.Enabled ? "~/Images/first-active.png" : "~/Images/first-inactive.png";
                    PreviousLink.Enabled = !recordId.Equals(1);
                    PreviousImageLink.Enabled = PreviousLink.Enabled;
                    PreviousImageLink.ImageUrl = PreviousImageLink.Enabled ? "~/Images/previous-active.png" : "~/Images/previous-inactive.png";

                    FirstLink.NavigateUrl = string.Format("{0}?{1}={2}", Request.FilePath, recordIdentifier, 1);
                    FirstImageLink.NavigateUrl = FirstLink.NavigateUrl;
                    PreviousLink.NavigateUrl = string.Format("{0}?{1}={2}", Request.FilePath, recordIdentifier, Math.Max(1, recordId - 1));
                    PreviousImageLink.NavigateUrl = PreviousLink.NavigateUrl;
                    NextLink.NavigateUrl = string.Format("{0}?{1}={2}", Request.FilePath, recordIdentifier, Math.Min(recordId + 1, recordCount));
                    NextImageLink.NavigateUrl = NextLink.NavigateUrl;

                    EntriesLabel.Text = string.Format("Entry {0} of {1}", recordId, recordCount);

                    ListLink.NavigateUrl = string.Format("{0}?PageNumber={1}&PageSize={2}", Request.FilePath, (1 + ((recordCount - recordId) / PageSize)), PageSize);
                }
                else
                {
                    Visible = false;
                }
            }
        }
    }
}